package com.hangwei.aicabinet.manager;

import android.util.Log;

import com.hangwei.aicabinet.net.MySubscriber;
import com.hangwei.aicabinet.net.ResponseInfo;
import com.hangwei.aicabinet.net.ResponseInfoAPI;
import com.hangwei.aicabinet.util.LogUtil;
import com.hangwei.aicabinet.util.ToastUtil;

import rx.Observable;
import rx.Subscription;


/**
 * author: 张龙臣
 * date: 2018-08-13.
 */

public class NetManager {
    private static NetManager instance;
    private static ResponseInfoAPI mResponseInfoAPI;

    private NetManager() {
    }

    public static ResponseInfoAPI getResponseInfoAPI() {
        if (mResponseInfoAPI == null) {
            synchronized (NetManager.class) {
                if (mResponseInfoAPI == null) {
                    mResponseInfoAPI = Retrofit2Manager.getInstance().create(ResponseInfoAPI.class);
                }
            }
        }
        return mResponseInfoAPI;
    }

    public static NetManager getInstance() {
        if (instance == null) {
            synchronized (NetManager.class) {
                if (instance == null) {
                    instance = new NetManager();
                }
            }
        }
        return instance;
    }

    public <T> Subscription doSubscribe(Model<T> model) {
        return model.execute();
    }

    public abstract static class Model<T> {

        private Subscription execute() {

            return RXManager.getInstance().doSubscribe(getObservable(), new MySubscriber<ResponseInfo<T>>() {
                @Override
                public void onNext(ResponseInfo<T> tResponseInfo) {
                    super.onNext(tResponseInfo);
                    if (tResponseInfo.code == 0) {
                        LogUtil.d("解析成功 格式正确 code == 0 数据正常 !");
                        Model.this.onNext(tResponseInfo.data);
                    } else if (tResponseInfo.code == 2) {
                        LogUtil.e("解析成功 格式正确 code == 2 归还错误 !   ------------");
                        Model.this.onError(tResponseInfo.msg);
                        Model.this.onError(tResponseInfo.msg, 2);
                    } else if (tResponseInfo.code == 5 || tResponseInfo.code == 3) {
                        LogUtil.e("解析成功 格式正确 code == 2 归还错误 !   ------------");
                        Model.this.onError(tResponseInfo.msg);
                        Model.this.onError(tResponseInfo.msg, 5);
                    } else {
                        //返回数据格式正确,但flag显示不是正确想要的数据(一般msg中会给出错误情况)
                        LogUtil.e("解析成功 格式正确 code != 0 无正常数据 !   ------------");
                        Model.this.onError(tResponseInfo.msg);
                    }
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);//直接请求错误,根本没有拿到 ResponseInfo<T> 格式数据, 基本断定为网络错误或解析格式错误
                    Model.this.onError(null);
                }

            });
        }

        /**
         * 请求获取到了数据,但服务器返回数据显示不是成功数据
         */
        private void onError(String msg) {
            ToastUtil.showCenterToast(msg == null ? "网络连接超时" : msg);
            Log.e(this.getClass().getName(), msg == null ? "网络连接超时" : msg);
            onError();
        }

        protected abstract Observable<ResponseInfo<T>> getObservable();

        protected abstract void onNext(T data);

        /**
         * 错误回调的额外处理
         */
        protected abstract void onError();

        protected void onError(String msg, int code) {
        }
    }
}
